package Test.Test3;

import java.util.*;

public class Exam3 {
    private String palyer1;
    private String palyer2;
    private String player3;
    private String lastcards = "最后的底牌";
    private List A = new ArrayList();//存放玩家一的牌
    private List B = new ArrayList();//存放玩家二的牌
    private List C = new ArrayList();//存放玩家三的牌
    private List lastCard = new ArrayList();//存放最后三张底牌
    Map map = null;

    public Exam3(String palyer1, String palyer2, String player3,Map map) {
        this.palyer1 = palyer1;//获取玩家一的姓名
        this.palyer2 = palyer2;//获取玩家二的姓名
        this.player3 = player3;//获取玩家三的姓名
        this.map = Card();//创建一幅新的扑克
    }

    /**
     * 生成一幅新的扑克牌
     * @return 返回一幅新的扑克
     */
    public static Map Card() {
        String num[] = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};//存放牌的大小
        String color[] = {"♠", "♣", "♥", "♦"};//存放牌的花色
        Map<Integer, String> card = new HashMap<>();//创建一个HashMap集合存储扑克
        card.put(54, "大王");//把大王放进扑克
        card.put(53, "小王");//把小王放进扑克
        int count = 1;
        for (int i = 0; i < num.length; i++) {//利用一个循环把剩下的52张牌放进集合
            for (int j = 0; j < color.length; j++) {
                card.put(count, color[j] + num[i]);
                count++;
            }
        }

        return card;//返回新的扑克
    }

    /**
     * 发牌
     */
    public void putCard() {

        List<Integer> list = new ArrayList<Integer>(map.keySet());//创建一个List集合存放集合的序号
        Collections.shuffle(list);//打乱序号顺序
        lastCard = new ArrayList<>(list.subList(list.size() - 3, list.size()));//获取三张底牌
        list.removeAll(lastCard);//把三张底牌的序号从集合中删除
        Iterator<Integer> iterator = list.iterator();//获取集合的迭代器
        int a, b, c;//创建变量记录迭代的元素
        while (iterator.hasNext()) {//对集合进行迭代
            a = iterator.next();
            b = iterator.next();
            c = iterator.next();
            A.add(a);
            System.out.println("给" + palyer1 + "发了一张" + map.get(a));
            B.add(b);
            System.out.println("给" + palyer2 + "发了一张" + map.get(b));
            C.add(c);
            System.out.println("给" + palyer1 + "发了一张" + map.get(c));
        }

        show(A, palyer1);//输出玩家一的牌
        show(B, palyer2);//输出玩家二的牌
        show(C, player3);//输出玩家三的牌
        show(lastCard, lastcards);//输出三张底牌


    }

    /**
     * 将牌转换成String格式输出
     * @param list 玩家的牌
     * @param name 玩家的姓名
     */
    public void show(List list, String name) {
        Collections.sort(list, new Comparator<Integer>() {//对牌进行排序
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        Iterator<Integer> iterator = list.iterator();
        String s = name + "[";
        while (iterator.hasNext()) {
            s = s + map.get(iterator.next());
            if (iterator.hasNext()) {
                s = s + ",";
            }
        }
        s += "]";
        System.out.println(s);


    }

    public static void main(String[] args) {
       Map map  = new HashMap();
        Exam3 exam3 = new Exam3("玩家一", "玩家二", "玩家三",map);
        exam3.putCard();
    }
}
